<template>
  <div class="app-container">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" v-show="showSearch" label-width="100px" class="demo-dynamic">
      <el-form-item
        v-for="(domain, index) in dynamicValidateForm.domains"
        :label="'公司' + index"
        :key="domain.key"
        :prop="'searchKey' + index"
        :rules="{
          required: true, message: '请输入公司名、注册号、统一社会信用代码', trigger: 'blur'
        }">
        <el-row>
          <el-col :span="9">
            <el-input v-model="domain.value"></el-input>
          </el-col>
          <el-col :span="3">
            <el-button @click.prevent="removeDomain(domain)">删除</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery(dynamicValidateForm.domains)">搜索</el-button>
        <el-button @click="addDomain">新增搜索输入框</el-button>
        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="keyList" @selection-change="handleSelectionChange">
      <el-table-column label="公司1" align="center" prop="searchKey1" />
      <el-table-column label="公司2" align="center" prop="searchKey2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="clickDetail(scope.row)"
          >查看详情</el-button>
      </template>
      </el-table-column>
    </el-table>
    <br>
    <el-button type="text" @click="clickOne">历史记录</el-button>
    <div v-if="flag">
      <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:subjectList:remove']"
        >删除</el-button>
      </el-col>
    </el-row> -->
      <el-table v-loading="loading" :data="keyAllList" >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="公司1" align="center" prop="searchKey1" />
        <el-table-column label="公司2" align="center" prop="searchKey2" />
        <el-table-column label="搜索时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="clickDetail(scope.row)"
            >查看详情</el-button>
        </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { findAllKey, delSubjectList, searchKey, findKeyList} from "@/api/system/subjectList";

export default {
  name: "subject",
  data() {
    return {
      dynamicValidateForm: {
        domains: [{
          value: ''
        }],
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,

      flag: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 同一条关联关系上的节点信息表格数据
      keyAllList: [],
      keyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
    };
  },
  created() {
      this.getList();
    },
  methods: {
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: '',
        key: Date.now()
      });
    },
    clickOne() {
      if(this.flag == false){
        this.flag = true;
        this.getList();
      }else{
        this.flag = false;
      }
    },
    clickDetail(row) {
      this.$router.push({
        path: '/system/subject/relation/index',
        query: {
          //要传的参数
          subjectId: row.subjectId,
        },
    })
    },
    /** 查询同一条关联关系上的节点信息列表 */
    getList() {
      this.loading = true;
      findAllKey().then(response => {
        this.keyAllList = response.data;
        this.loading = false;
      });
    },
    getList1() {
      this.loading = true;
      findKeyList(this.dynamicValidateForm.domains).then(response => {
        this.keyList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery(domain) {
      searchKey(domain).then(response => {
        this.loading = false;
      }).catch(() => {
      }).finally(() => {
        this.$modal.msgSuccess("查询完成,结果如下");
        this.loading = false;
        this.getList1(domain);
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除同一条关联关系上的节点信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSubjectList(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
